package com.example.wangning.mycarofcountry.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.wangning.mycarofcountry.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/20.
 */
public class RegisterFragment extends Fragment implements View.OnClickListener, TextWatcher {
    private EditText loginName_Ed, registerPassword_Ed, registerRepeatPassword_Ed;
    private Button register_btn;
    private SharedPreferences sharedPreferences;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loginName_Ed = (EditText) view.findViewById(R.id.loginName_Ed);
        registerPassword_Ed = (EditText) view.findViewById(R.id.registerPassword_Ed);
        registerRepeatPassword_Ed = (EditText) view.findViewById(R.id.registerRepeatPassword_Ed);
        register_btn = (Button) view.findViewById(R.id.register_btn);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        register_btn.setOnClickListener(this);
        loginName_Ed.addTextChangedListener(this);
        registerPassword_Ed.addTextChangedListener(this);
        registerRepeatPassword_Ed.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_btn:
                if (registerRepeatPassword_Ed.getText().toString().equals(registerPassword_Ed.getText().toString())) {
                    Login login = new Login();
                    login.setLoginName(loginName_Ed.getText().toString());
                    login.setPassword(registerPassword_Ed.getText().toString());
                    List<Login> logins = new ArrayList<>();
                    logins.add(login);
                    sharedPreferences = getActivity().getSharedPreferences("word", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("loginName",loginName_Ed.getText().toString());
                    editor.putString("password",registerPassword_Ed.getText().toString());
                    editor.commit();
                    Intent intent = new Intent();
                    intent.putExtra("loginName",loginName_Ed.getText().toString());
                    getActivity().setResult(200, intent);
                    getActivity().finish();
                }
                break;
        }


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (loginName_Ed.getText().toString() != null &&
                loginName_Ed.getText().toString().length() > 0
                && registerPassword_Ed.getText().toString() != null &&
                registerPassword_Ed.getText().toString().length() > 0
                && registerRepeatPassword_Ed.getText().toString() != null &&
                registerRepeatPassword_Ed.getText().toString().length() > 0) {
            register_btn.setEnabled(true);
            register_btn.setBackgroundResource(R.color.blue_color);
        }
    }
}
